package com.leo.foodmaster.admin.recipe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leo.foodmaster.admin.recipe.entity.RecipeTemplateDetailsEntity;
import com.leo.foodmaster.admin.recipe.service.IRecipeTemplateDetailsService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.WebUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.recipe.dto.RecipeTemplateDTO;
import com.leo.foodmaster.admin.recipe.entity.RecipeTemplateEntity;
import com.leo.foodmaster.admin.recipe.mapper.RecipeTemplateMapper;
import com.leo.foodmaster.admin.recipe.service.IRecipeTemplateService;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜品模板
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-10-29
 */
@Service
@AllArgsConstructor
public class IRecipeTemplateServiceImpl extends ServiceImpl<RecipeTemplateMapper, RecipeTemplateEntity> implements IRecipeTemplateService {

	private final RecipeTemplateMapper recipeTemplateMapper;
	private final IRecipeTemplateDetailsService recipeTemplateDetailsService;

	/**
	 * 条件分页查询
	 * @param page 分页条件
	 * @param recipeTypeId 菜品类型Id
	 * @param name 菜品名称
	 * @param status 菜品状态
	 * @return IPage<RecipeTemplateEntity>
	 */
	@Override
	public IPage<RecipeTemplateDTO> pageList(Page<Object> page, Long recipeTypeId, String name, String status) {
		Long organId = WebUtils.getUser().getOrganId();
		IPage<RecipeTemplateDTO> pageList = recipeTemplateMapper.pageList(page, organId, recipeTypeId, name, status);
		List<RecipeTemplateDTO> records = pageList.getRecords();
		for (RecipeTemplateDTO record : records) {
			List<RecipeTemplateDetailsEntity> list = recipeTemplateDetailsService.list(Wrappers.lambdaQuery(RecipeTemplateDetailsEntity.class)
					.eq(RecipeTemplateDetailsEntity::getTemplateId, record.getId()));
			record.setDetailList(list);
		}
		return pageList;
	}

	/**
	 * 新增菜品模板
	 * @param dto 菜品信息
	 * @return Boolean
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean addRecipeTemplate(RecipeTemplateDTO dto) {
		Long organId = WebUtils.getUser().getOrganId();
		// 新增菜品模板主表
		RecipeTemplateEntity recipeTemplateEntity = new RecipeTemplateEntity();
		BeanUtil.copyProperties(dto, recipeTemplateEntity);
		recipeTemplateEntity.setOrganId(organId);
		save(recipeTemplateEntity);
		// 新增菜品模板细表
		List<RecipeTemplateDetailsEntity> detailList = dto.getDetailList();
		detailList.forEach(item ->{
			item.setOrganId(organId);
			item.setTemplateId(recipeTemplateEntity.getId());
		});
		return recipeTemplateDetailsService.saveBatch(detailList);
	}

	/**
	 * 修改菜品模板信息
	 * @param dto 菜品模板信息
	 * @return Boolean
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean updateRecipeTemplate(RecipeTemplateDTO dto) {
		Long organId = WebUtils.getUser().getOrganId();
		// 修改主表信息
		RecipeTemplateEntity recipeTemplateEntity = new RecipeTemplateEntity();
		BeanUtil.copyProperties(dto, recipeTemplateEntity);
		updateById(recipeTemplateEntity);

		// 删除原来的细表信息
		recipeTemplateDetailsService.remove(Wrappers.lambdaUpdate(RecipeTemplateDetailsEntity.class)
				.eq(RecipeTemplateDetailsEntity::getTemplateId, recipeTemplateEntity.getId()));
		// 新增细表信息
		List<RecipeTemplateDetailsEntity> detailList = dto.getDetailList();
		detailList.forEach(item ->{
			item.setOrganId(organId);
			item.setTemplateId(recipeTemplateEntity.getId());
		});
		return recipeTemplateDetailsService.saveBatch(detailList);
	}

	/**
	 * 删除菜品模板
	 * @param idList id列表
	 * @return Result<Object>
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result<Object> deleteBatchIds(List<Long> idList) {
		// 删除细表
		for (Long id : idList) {
			recipeTemplateDetailsService.remove(Wrappers.lambdaUpdate(RecipeTemplateDetailsEntity.class)
					.eq(RecipeTemplateDetailsEntity::getTemplateId, id));
		}
		// 删除主表
		recipeTemplateMapper.deleteBatchIds(idList);
		return Result.success();
	}
}
